package com.wutong.erb.exception.base;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
 * 业务异常类
 * @author wutong
 */
@Getter
public class BusinessException extends RuntimeException {
    private final int code;

    private static final Map<Integer, BusinessException> BUSINESS_EXCEPTION_MAP = new HashMap<>();

    public BusinessException(int code, String message) {
        super(message);
        this.code = code;
        if (BUSINESS_EXCEPTION_MAP.get(code) == null) {
            BUSINESS_EXCEPTION_MAP.put(code, this);
        } else {
            throw new IllegalArgumentException("存在相同的异常码: " + code + " - " + BUSINESS_EXCEPTION_MAP.get(code).getClass().getSimpleName() + " <-> " + this.getClass().getSimpleName());
        }
    }

    public static BusinessException getException(int code) {
        return BUSINESS_EXCEPTION_MAP.get(code);
    }

}
